﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Common;

public class PageBase : System.Web.UI.Page
{
    #region 构造
    public PageBase()
    {

    }
    #endregion

    #region 属性
    /// <summary>
    /// 用户登录名
    /// </summary>
    public string UserName
    {
        get
        {
            return GetParaValue("Username");
        }
    }
    /// <summary>
    /// 用户角色 Id, 多个角色 | 为间隔符
    /// </summary>
    public string RoleId
    {
        get
        {
            return BLL.SystemUserBase.GetRolesBySystemUserId(this.SystemUserId);
        }
    }
    /// <summary>
    /// 用户ID
    /// </summary>
    public string SystemUserId
    {
        get
        {
            return UserInfo.SystemUserId;
        }
    }
    /// <summary>
    /// 用户组ID,角色ID
    /// </summary>
    public Model.SystemUserBase UserInfo
    {
        get
        {
            Model.SystemUserBase _obj = BLL.SystemUserBase.GetObjByUsername(UserName);
            return _obj;
        }
    }
    #endregion

    #region 方法
    /// <summary>
    /// 从ViewState中获取某个属性的值。如果该属性不存在，返回空字符串。
    /// </summary>
    /// <param name="PropertyName">属性名称</param>
    /// <returns>属性值（属性不存在时返回空字符串）</returns>
    public string GetViewState(string PropertyName)
    {
        try
        {
            return ViewState[PropertyName].ToString();
        }
        catch (NullReferenceException)
        {
            return "";
        }
    }

    /// <summary>
    /// 设置ViewState中某个属性的值
    /// </summary>
    /// <param name="PropertyName">属性名称</param>
    /// <param name="PropertyValue">属性值</param>
    public void SetViewState(string PropertyName, string PropertyValue)
    {
        ViewState[PropertyName] = PropertyValue;
    }

    public void PageBegin()
    {
        if (string.IsNullOrEmpty(UserName))
        {
            Response.Redirect("http://" + RequestBase.GetCurrentFullHost() + ConfigurationManager.ConnectionStrings["ORG_UNIQUE_NAME"].ToString() + "/admin/signin.aspx");
            Response.End();
        }
    }
    /// <summary>
    /// 匹配模块名称，判断页面加载的权限
    /// </summary>
    public void PageBegin(string functionname)
    {
        bool _valadate = false;

        //if (UserName.ToLower() == "admin")
        //    _valadate = true;
        //else
        //{        //}
        string _roleid = this.RoleId;
        if (string.IsNullOrEmpty(_roleid))
            Response.Redirect("../unauthorized.htm?errcode=4");

        for (int i = 0; i < _roleid.Split('|').Length; i++)
        {
            Model.SystemsRolePrivileges[] _privileges = BLL.SystemRoles.GetFunctionPrivilegesByRoleId(_roleid.Split('|')[i]);

            if (_privileges == null)
                _valadate = false;

            foreach (Model.SystemsRolePrivileges _pi in _privileges)
            {
                if (_pi.FunctionName.ToLower() == functionname.ToLower())
                {
                    _valadate = true;
                    break;
                }

                if (_valadate)
                    break;
            }

            if (_valadate)
                break;
        }


        if (!_valadate)
            Response.Redirect("../unauthorized.htm?errcode=4");
    }

    /// <summary>
    /// 匹配模块名称、权限值, 判断页面加载的权限
    /// </summary>
    public void PageBegin(string functionname, int lineNum)
    {
        //if (UserName.ToLower() == "admin")
        //    return;

        bool _valadate = false;

        string _roleid = this.RoleId;
        if (string.IsNullOrEmpty(_roleid))
            Response.Redirect("../unauthorized.htm?errcode=4");

        for (int i = 0; i < _roleid.Split('|').Length; i++)
        {
            Model.SystemsRolePrivileges[] _privileges = BLL.SystemRoles.GetFunctionPrivilegesByRoleId(_roleid.Split('|')[i]);

            if (_privileges == null)
                _valadate = false;

            foreach (Model.SystemsRolePrivileges _pi in _privileges)
            {
                if (_pi.FunctionName.ToLower() == functionname.ToLower())
                    if (ValidatePrivilege(lineNum, int.Parse(_pi.FunctionDepthMask)))
                        _valadate = true;
            }

            if (_valadate)
                break;
        }

        if (!_valadate)
            Response.Redirect("../unauthorized.htm?errcode=4");
    }

    /// <summary>
    /// 判断是否有模块的操作权限, 是, True
    /// lineNum 模块操作权限的序号
    /// </summary>
    public bool Validate(string functionname, int lineNum)
    {
        //if (UserName.ToLower() == "admin")
        //    return true;

        bool _valadate = false;

        string _roleid = this.RoleId;
        if (string.IsNullOrEmpty(_roleid))
            return false;

        for (int i = 0; i < _roleid.Split('|').Length; i++)
        {
            Model.SystemsRolePrivileges[] _privileges = BLL.SystemRoles.GetFunctionPrivilegesByRoleId(_roleid.Split('|')[i]);

            if (_privileges == null)
                _valadate = false;

            foreach (Model.SystemsRolePrivileges _pi in _privileges)
            {
                if (_pi.FunctionName.ToLower() == functionname.ToLower())
                    if (ValidatePrivilege(lineNum, int.Parse(_pi.FunctionDepthMask)))
                        _valadate = true;
            }

            if (_valadate)
                break;
        }

        return _valadate;
    }

    /// <summary>
    /// 判断是否有模块的操作权限, 是, True
    /// lineNum 模块操作权限的序号
    /// _roleid 角色Id
    /// </summary>
    public bool Validate(string functionname, int lineNum, string _roleid)
    {
        //if (UserName.ToLower() == "admin")
        //    return true;

        bool _valadate = false;

        if (string.IsNullOrEmpty(_roleid))
            return false;

        Model.SystemsRolePrivileges[] _privileges = BLL.SystemRoles.GetFunctionPrivilegesByRoleId(_roleid);

        if (_privileges == null)
            _valadate = false;

        foreach (Model.SystemsRolePrivileges _pi in _privileges)
        {
            if (_pi.FunctionName.ToLower() == functionname.ToLower())
                if (ValidatePrivilege(lineNum, int.Parse(_pi.FunctionDepthMask)))
                    _valadate = true;
        }

        return _valadate;
    }

    /// <summary>
    /// 获取角色权限明细的深度,无权限明细 返回 X; 有,返回类型 : B 用户, L 部门, G 全部
    /// lineNum 模块操作权限的序号
    /// </summary>
    public string DepthLevel(string functionname, int lineNum)
    {
        //if (UserName.ToLower() == "admin")
        //    return "G";

        string _depth = BLL.SystemRoles.GetPrivilegeDepthByParams(this.RoleId, functionname.ToLower(), lineNum);

        return _depth;
    }
    /// <summary>
    /// 获取页面参数的值
    /// </summary>
    public string GetParaValue(string paraName)
    {
        string strValue = "";
        //如果Session中能够找到相应的参数值，则返回参数值
        try
        {
            strValue = StringEncrypt.DecryptString(HttpUtility.UrlDecode(System.Web.HttpContext.Current.Request.Cookies["brighterfutureled_com"][paraName]));  
        }
        catch (Exception err)
        {
            err.ToString();
            strValue = "";
        }

        return strValue;
    }
    /// <summary>
    /// 判断操作权限
    /// </summary>
    /// <param name="lineNum">操作权限值,流水号模式</param>
    /// <param name="RightsValue">模块的总权限值</param>
    private bool ValidatePrivilege(int lineNum, int privilegeValue)
    {
        int purviewValue = (int)Math.Pow(2, lineNum);
        return (privilegeValue & purviewValue) == purviewValue;
    }
    #endregion
}

